/*
 * @Author: 深海有鱼 847094299@qq.com
 * @Date: 2025-04-03 22:50:01
 * @LastEditors: 深海有鱼 847094299@qq.com
 * @LastEditTime: 2025-08-25 11:43:32
 * @FilePath: \pure-admin-thin\src\api\taskApi.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { http } from "@/utils/http";
import { useStreamDownload } from "@/utils/useStreamDownload";

/** 分页获取当前用户的任务列表 */
export const getUserTasks = (params?: {
  page?: number;
  size?: number;
  serviceType?: string;
  country?: string;
  status?: string;
  keyword?: string;
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<any>("get", "/api/tasks", { params });
};

/** 创建普通任务（上传文件等参数通过 multipart/form-data 传递） */
// export const createTask = (data: FormData) => {
//   return http.request<any>("post", "/api/tasks", {
//     data,
//     headers: { "Content-Type": "multipart/form-data" },
//     hideMessage: true
//   });
// };
/** 与创建任务接口功能相同，但会跳过重复提交检测 */
export const createTaskWithoutDuplicateCheck = (data: FormData) => {
  return http.request<any>(
    "post",
    "/api/tasks/create-without-duplicate-check",
    {
      data,
      headers: { "Content-Type": "multipart/form-data" }
    }
  );
};

/** 创建高级任务（支持完整 TaskCreateDTO 参数及文件上传） */
export const createAdvancedTask = (data: FormData) => {
  return http.request<any>("post", "/api/tasks/advanced", { data });
};

/** 同步任务状态（用户主动调用） */
export const syncTaskStatus = () => {
  return http.request<any>("post", "/api/tasks/sync");
};
/** 同步单个任务状态（用户主动调用） */
export const syncSingleTaskStatus = (id: number) => {
  return http.request<any>("post", `/api/tasks/${id}/sync`);
};

/** 重新同步任务 */
export const resyncSingleTaskStatus = (id: number) => {
  return http.request<any>("post", `/api/tasks/${id}/resync`);
};

/** 获取任务详情 */
export const getTaskDetail = (taskId: number) => {
  return http.request<any>("get", `/api/tasks/${taskId}`);
};

/** 获取任务结果下载链接 */
export const getTaskResultUrl = (taskId: number) => {
  return http.request<any>("get", `/api/tasks/${taskId}/download`);
};

/** 直接下载任务结果文件 */
export const downloadFilterResultFile = (
  taskId: number,
  params: {
    activeDaysThreshold?: string | number;
  }
) => {
  const { download } = useStreamDownload();
  return download(`/api/tasks/${taskId}/filtered-download`, {
    method: "GET",
    params
  });
};

/** 直接下载任务小时结果文件 */
export const downloadHoursResultFile = (
  taskId: number,
  params: {
    recentHours?: string | number;
  }
) => {
  const { download } = useStreamDownload();
  return download(`/api/tasks/${taskId}/recent-hours-download`, {
    method: "GET",
    params
  });
};

/** 直接下载任务结果文件 */
export const downloadTaskResultFile = (
  taskId: number,
  params: {
    fileType: string;
  },
  csvKeepColumns?: string[]
) => {
  const { download } = useStreamDownload();
  return download(`/api/tasks/${taskId}/download-file`, {
    method: "GET",
    params,
    csvKeepColumns
  });
};

/** 直接下载手机号结果文件 */
export const downloadPhonesOnly = (taskId: number, noResult = false) => {
  const { download } = useStreamDownload();
  return download(`/api/tasks/${taskId}/phones-only`, {
    method: "GET",
    params: { noResult }
  });
};

/** 直接下载邮箱结果文件 */
export const downloadEmailsOnly = (taskId: number, noResult = false) => {
  const { download } = useStreamDownload();
  return download(`/api/tasks/${taskId}/emails-only`, {
    method: "GET",
    params: { noResult }
  });
};

// 小文件上传，文件大小小于10M
export const uploadDataFile = (data: FormData) => {
  return http.request<any>("post", "/api/tasks/upload/dataFile", {
    data,
    headers: { "Content-Type": "multipart/form-data" }
  });
};
// 校验文件信息
export const checkTaskInfo = (data: {
  name?: string;
  project: string;
  dataFile: string;
  dataFileName: string;
  country: string;
}) => {
  return http.request<any>("post", "/api/tasks/check", {
    data,
    headers: { "Content-Type": "multipart/form-data" }
  });
};
// 确认创建任务
export const createTask = (cacheKey: string) => {
  return http.request<any>("post", `/api/tasks/create/${cacheKey}`);
};

// 上传单个分片
export const uploadPart = (
  uploadId: string,
  data: FormData,
  signal?: AbortSignal
) => {
  return http.request<any>("post", `/api/tasks/upload/${uploadId}/part`, {
    data,
    headers: { "Content-Type": "multipart/form-data" },
    signal
  });
};

// 初始化分片上传
export const initMultipartUpload = (params: {
  fileName: string;
  fileSize: number;
  fileHash: string;
}) => {
  return http.request<any>("post", "/api/tasks/upload/init", {
    params
  });
};

// 计算缺失分片
export const getMissingParts = (uploadId: string) => {
  return http.request<any>("get", `/api/tasks/upload/${uploadId}/missing`);
};

// 完成分片上传
export const completeMultipartUpload = (
  uploadId: string,
  params: {
    fileName: string;
    fileSize: number;
  }
) => {
  return http.request<any>("post", `/api/tasks/upload/${uploadId}/complete`, {
    params
  });
};
// 取消上传
export const cancelUpload = (uploadId: string) => {
  return http.request<any>("post", `/api/tasks/upload/${uploadId}/cancel`);
};
